// 导入接口
import SpiderOptions from './interFaces/SpiderOptions'
const http = require('http')

export default class Spider {
  // 定义成员
  options: SpiderOptions
  // 接口定义options成员
  constructor(options: SpiderOptions = { url: '', method: 'get' }) {
    // 初始化成员
    this.options = options
    this.start()
  }
  start() {
    // 创建请求对象
    let req = http.request(this.options.url, {
      headers: this.options.headers,
      method:  this.options.method
    }, (res: any) => {
      let chunks = []
      res.on('data', (c: any) => chunks.push(c))

      res.on('end', () => {
        let result = Buffer.concat(chunks).toString('utf-8')
        console.log(result)
      })
    })

    // 发送请求
    req.end()
  }
}
